<?php
/**
 * @author Wlasciciel
 *
 */
class CompetitionsComponent extends Component {
	public $components = array('Uploads','Session');	
	public $controller = null;
	public $domain_id = null;

	public function initialize(Controller $controller) {
		$this->controller = $controller;
		$this->domain_id = $this->Session->read('Config.domain_id');
		if(is_null($this->domain_id)){
			$this->controller->loadModel('Domain');
			$domain = $this->controller->Domain->get_info(Configure::read('Config.domain'));
			$this->Session->write('Config.domain_id',$domain['Domain']['id']);
			$this->domain_id = $this->Session->read('Config.domain_id');
		}
	}

	public function getArticles(Array $selected_menu){
		$this->controller->loadModel('Article');
		$this->controller->loadModel('Upload');
		$this->controller->loadModel('Rating');
		$this->controller->loadModel('RatingValue');
		$this->controller->loadModel('Comment');
		$this->controller->loadModel('Post');
		$this->controller->Article->recursive = -1;
		$this->controller->Upload->recursive = -1;
		$this->controller->Rating->recursive = -1;
		$this->controller->RatingValue->recursive = -1;
		$this->controller->Comment->recursive = -1;
		$this->controller->Post->recursive = -1;
		$options = array();
		if(isset($selected_menu['s'])){
			// submenu
			$options['conditions'] = array('Article.submenu_id'=>$selected_menu['s'],'Article.status'=>1,'Article.domain_id'=>$this->domain_id);
	
		}
		if(isset($selected_menu['m'])){
			$options['conditions'] = array('Article.menuses_id'=>$selected_menu['m'],'Article.status'=>1,'Article.domain_id'=>$this->domain_id);
		}
		
// 		$articles = $this->controller->Article->find('all',$options);
		
		$options['limit']=5;
		$this->controller->paginate = $options;		
		$articles = $this->controller->paginate('Article');

		$options['joins'] = array(
			array('table' => 'articles_uploads',
							        'alias' => 'ArticlesUpload',
							        'type' => 'inner',
							        'conditions' => array(
							            'Upload.id = ArticlesUpload.upload_id'
			)
		),
		array('table' => 'articles',
							        'alias' => 'Article',
							        'type' => 'inner',
							        'conditions' => array(
							            'ArticlesUpload.article_id = Article.id'
			)
			)
		);

		$options['order'] = array(
									    'Upload.type'
		);
		
		
		$options_rating['joins'] = array(
		array('table' => 'rating_articles',
									        'alias' => 'RatingArticle',
									        'type' => 'inner',
									        'conditions' => array(
									            'Rating.id = RatingArticle.rating_id'
		)
		),
		array('table' => 'articles',
									        'alias' => 'Article',
									        'type' => 'inner',
									        'conditions' => array(
									            'RatingArticle.article_id = Article.id'
		)
		)
		);
		
		$options_rating['order'] = array(
											    'Rating.name'
		);
		
		$options_rating_values['joins'] = array(
				array('table' => 'articles',
									        'alias' => 'Article',
									        'type' => 'inner',
									        'conditions' => array(
									            'RatingValue.article_id = Article.id'
		)
		)
		);
		
		
		$options_comments['joins'] = array(
		array('table' => 'articles_comments',
											        'alias' => 'ArticlesComment',
											        'type' => 'inner',
											        'conditions' => array(
											            'Comment.id = ArticlesComment.comment_id'
		)
		),
		array('table' => 'articles',
											        'alias' => 'Article',
											        'type' => 'inner',
											        'conditions' => array(
											            'ArticlesComment.article_id = Article.id'
		)
		)
		);
		
		$options_comments['order'] = 'Comment.created DESC';
		$options_comments['limit'] = 2;
		
		$options_posts['joins'] = array(
		array('table' => 'posts_articles',
													        'alias' => 'PostsArticle',
													        'type' => 'inner',
													        'conditions' => array(
													            'Post.id = PostsArticle.post_id'
		)
		),
		array('table' => 'articles',
													        'alias' => 'Article',
													        'type' => 'inner',
													        'conditions' => array(
													            'PostsArticle.article_id = Article.id'
		)
		)
		);
		
// 		$title_for_layout = ' ';
// 		$keywords = ' ';
// 		$description = ' ';

		
		foreach ($articles as $key => $article) {
			
// 			$title_for_layout .= $article['Article']['name'];
// 			$keywords .= $article['Article']['keywords'];
// 			$description .= $article['Article']['descriptions'];
			
			$options['conditions'] = array( 'Article.id' => $article['Article']['id'],'Article.domain_id'=>$this->domain_id );
			$files = $this->controller->Upload->find('all',$options);
			
			$options_rating['conditions'] = array( 'Article.id' => $article['Article']['id'], 'Rating.status' => 1,'Article.domain_id'=>$this->domain_id );
			$ratings = $this->controller->Rating->find('all',$options_rating);
			
			$options_rating_values['conditions'] = array( 'Article.id' => $article['Article']['id'],'Article.domain_id'=>$this->domain_id);
			$rating_values = $this->controller->RatingValue->find('all',$options_rating_values);			
			$total_rating = count($rating_values);
			$rating_values = $this->prepare_rating_values($rating_values,$total_rating);
			$total_calculate = array();
			$total_calculate['count'] = 0;
			$total_calculate['sum'] = 0;
			$i = 1;
			foreach ($rating_values as $rating_id => $rrow) {
				$total_calculate['count'] = $total_calculate['count'] + $rrow['count'];
				$total_calculate['sum'] = $total_calculate['sum'] + $rrow['sum'];
				if($total_calculate['count'] != 0) $total_calculate['serd'] = round( $total_calculate['sum'] / $total_calculate['count']  ,0);
				if($total_calculate['count'] != 0) $total_calculate['proc'] = $i  * 100 /$total_calculate['count'];
				
				$i++;
			}
			$rating_values['total'] = $total_calculate;
// 			debug($rating_values);	
				
			$options_comments['conditions'] = array( 'Article.id' => $article['Article']['id'], 'Comment.status' => 1,'Article.domain_id'=>$this->domain_id );
			$comments = $this->controller->Comment->find('all',$options_comments);
			
			$options_posts['conditions'] = array( 'Article.id' => $article['Article']['id'], 'Post.status' => 1,'Article.domain_id'=>$this->domain_id );
			$posts = $this->controller->Post->find('all',$options_posts);
			
			$gallery = array();
			$download = array();
			if(is_array($files)){				
				foreach ($files as $key2 => $file) {					
					$this->Uploads->upload_file(Configure::read('Config.panel_uploads_folder').$file['Upload']['name'], Configure::read('Config.loacl_uploads_folder'), $file['Upload']['name'], false);
					if(
						$file['Upload']['type'] == 'image/jpeg' ||
						$file['Upload']['type'] == 'image/jpg' ||
						$file['Upload']['type'] == 'image/png' ||
						$file['Upload']['type'] == 'image/gif'
					)
					{
						array_push($gallery, $file);
					}
					if(	$file['Upload']['type'] == 'application/pdf' )
					{
						array_push($download, $file);
					}
				}
			}
			$articles[$key]['Gallery'] = $gallery;
			$articles[$key]['Download'] = $download;
			$articles[$key]['Rating'] = $ratings;
			$articles[$key]['RatingValue'] = $rating_values;
			$articles[$key]['RatingTotal'] = $total_rating;
			$articles[$key]['Comment'] = $comments;
			$articles[$key]['Post'] = $posts;
		}		
		$this->controller->set(compact('articles', 'title_for_layout','keywords','description'));
		return true;
	}
	

	public function articles_homepage_values(){
		$options_rating['joins'] = array(
		array('table' => 'rating_articles',
									        'alias' => 'RatingArticle',
									        'type' => 'inner',
									        'conditions' => array(
									            'Rating.id = RatingArticle.rating_id'
		)
		),
		array('table' => 'articles',
									        'alias' => 'Article',
									        'type' => 'inner',
									        'conditions' => array(
									            'RatingArticle.article_id = Article.id'
		)
		)
		);
		
		$options_rating['order'] = array(
											    'Rating.name'
		);
		
		$options_rating_values['joins'] = array(
				array('table' => 'articles',
						'alias' => 'Article',
						'type' => 'inner',
						'conditions' => array(
								'RatingValue.article_id = Article.id'
						)
				),
				array('table' => 'ratings',
						'alias' => 'Rating',
						'type' => 'inner',
						'conditions' => array(
								'RatingValue.rating_id = Rating.id'
						)
				),
				array('table' => 'rating_groups',
						'alias' => 'RatingGroup',
						'type' => 'inner',
						'conditions' => array(
								'Rating.rating_group_id = RatingGroup.id'
						)
				)
		);

		$options_posts['joins'] = array(
				array('table' => 'posts_articles',
						'alias' => 'PostsArticle',
						'type' => 'inner',
						'conditions' => array(
								'Post.id = PostsArticle.post_id'
						)
				),
				array('table' => 'articles',
						'alias' => 'Article',
						'type' => 'inner',
						'conditions' => array(
								'PostsArticle.article_id = Article.id'
						)
				)
		);
		$this->controller->loadModel('Rating');
		$this->controller->loadModel('RatingValue');
		$this->controller->loadModel('RatingGroup');
		$this->controller->loadModel('Post');
		$this->controller->Rating->recursive = -1;
		$this->controller->RatingValue->recursive = -1;
		$this->controller->RatingGroup->recursive = -1;
		$this->controller->Post->recursive = -1;
		
		$options_rating['conditions'] = array( 'Rating.status' => 1,'Article.domain_id'=>$this->domain_id );
		$options_rating['group'] = array( 'Rating.id');
		$options_rating['order'] = array( 'Rating.rating_group_id','Rating.id');
		$options_rating['fields'] = array( 'Rating.id','Rating.name');

		$ratings = $this->controller->Rating->find('list');

		$options_rating_values['conditions'] = array('Article.domain_id'=>$this->domain_id);
		$options_rating_values['fields'] = array('Article.name','Article.slug','Article.name','RatingValue.rating_value','RatingValue.rating_id','RatingValue.article_id','Rating.rating_group_id');
		
		$rating_groups = $this->controller->RatingGroup->find('list');
		
		$options_rating_values_best = $options_rating_values;
		$options_rating_values_best['group'] = array('Rating.rating_group_id','RatingValue.article_id');
		$options_rating_values_best['fields'] = array('SUM(RatingValue.rating_value) AS max_value','RatingValue.article_id','Rating.rating_group_id');
		$options_rating_values_best['order'] = 'max_value DESC';
		
		$best_articles = array();
		$best_offerts = array();
		foreach ($rating_groups as $group_id => $group_name) {
			// best ratings
			$options_rating_values_best['conditions'] = array('Article.domain_id'=>$this->domain_id,'RatingGroup.id'=>$group_id);
			$rating_values_best = $this->controller->RatingValue->find('first',$options_rating_values_best);
			$best_articles[$rating_values_best['RatingValue']['article_id']] = $rating_values_best['RatingValue']['article_id'];
			// best articles
			$options_rating_values_best['conditions'] = array('Article.domain_id'=>$this->domain_id,'RatingGroup.id'=>$group_id);
			$options_rating_values_best['fields'] = array('SUM(RatingValue.rating_value) AS max_value','Article.name','Article.slug','Article.name','RatingValue.rating_value','RatingValue.rating_id','RatingValue.article_id','Rating.rating_group_id');
			$options_rating_values_best['limit'] = 5;
			$rating_values_best = $this->controller->RatingValue->find('all',$options_rating_values_best);
			$best_offerts[$group_id] = $rating_values_best;
		}		
		
		$options_rating_values['conditions'] = array('Article.domain_id'=>$this->domain_id, 'Article.id'=>$best_articles);		
		$rating_values = $this->controller->RatingValue->find('all',$options_rating_values);
		
		$total_rating = count($rating_values);
		$rating_values = $this->prepare_homepage_rating_values($rating_values,$total_rating);
	
		
		$options_posts['conditions'] = array( 'Post.status' => 1,'Article.domain_id'=>$this->domain_id );
		$options_posts['limit'] = 4;
		$options_posts['fields'] = array('Post.user_id','Post.name','Post.content','Post.modified','Article.slug');
		$posts = $this->controller->Post->find('all',$options_posts);
		
		$page_index =  array('Rating'=>$ratings,'RatingValue'=>$rating_values,'RatingGroup'=>$rating_groups,'BestArticle'=>$best_offerts,'Post'=>$posts, 'RatingTotal'=> $total_rating);
		
		$title_for_layout = Configure::read('cfg.homepage_title');
		$keywords = Configure::read('cfg.homepage_keywords');
		$description = Configure::read('cfg.homepage_description');
		$this->controller->set(compact('page_index', 'title_for_layout','keywords','description'));
	}
	
	public  function prepare_rating_values($rating_values,$total){
		$values = array();
		
		foreach ($rating_values as $key => $rv) {
			$count = (isset($values[$rv['RatingValue']['rating_id']]['count'])) ?$values[$rv['RatingValue']['rating_id']]['count'] : 0;
			$sum = (isset($values[$rv['RatingValue']['rating_id']]['sum'])) ? $values[$rv['RatingValue']['rating_id']]['sum'] : 0;
			$values[$rv['RatingValue']['rating_id']]['count'] = $count + 1;
			$values[$rv['RatingValue']['rating_id']]['sum'] = $sum + $rv['RatingValue']['rating_value'];
			$values[$rv['RatingValue']['rating_id']]['serd'] = round(($sum + $rv['RatingValue']['rating_value']) / ($count + 1)  ,0);
			$values[$rv['RatingValue']['rating_id']]['proc'] = ($count + 1) * 100 /$total;
			$values[$rv['RatingValue']['rating_id']]['total'] = $total;
		}
		return $values;
	}
	
	public  function prepare_homepage_rating_values($rating_values,$total){
		$values = array();
	
		foreach ($rating_values as $key => $rv) {
			$count = (isset($values[$rv['Rating']['rating_group_id']][$rv['RatingValue']['rating_id']]['count'])) ?$values[$rv['Rating']['rating_group_id']][$rv['RatingValue']['rating_id']]['count'] : 0;
			$sum = (isset($values[$rv['Rating']['rating_group_id']][$rv['RatingValue']['rating_id']]['sum'])) ? $values[$rv['Rating']['rating_group_id']][$rv['RatingValue']['rating_id']]['sum'] : 0;
			$values[$rv['Rating']['rating_group_id']][$rv['RatingValue']['rating_id']]['count'] = $count + 1;
			$values[$rv['Rating']['rating_group_id']][$rv['RatingValue']['rating_id']]['sum'] = $sum + $rv['RatingValue']['rating_value'];
			$values[$rv['Rating']['rating_group_id']][$rv['RatingValue']['rating_id']]['serd'] = round(($sum + $rv['RatingValue']['rating_value']) / ($count + 1)  ,0);
			$values[$rv['Rating']['rating_group_id']][$rv['RatingValue']['rating_id']]['proc'] = ($count + 1) * 100 /$total;
			$values[$rv['Rating']['rating_group_id']][$rv['RatingValue']['rating_id']]['total'] = $total;
			$values[$rv['Rating']['rating_group_id']][$rv['RatingValue']['rating_id']]['article_name'] = $rv['Article']['name'];
			$values[$rv['Rating']['rating_group_id']][$rv['RatingValue']['rating_id']]['article_slug'] = $rv['Article']['slug'];
			$values[$rv['Rating']['rating_group_id']][$rv['RatingValue']['rating_id']]['article_id'] = $rv['RatingValue']['article_id'];
		}
		return $values;
	}

}